-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Do not materialise X in [X; 0] when X is unsizing a const #145277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Do not materialise X in [X; 0] when X is unsizing a const #145277
Conversation
r? @nnethercote rustbot has assigned @nnethercote. Use |
This comment has been minimized.
This comment has been minimized.
6a64501
to
b7b9519
Compare
A question to @traviscross ... Should we also need to run this through |
Well, let's see. It looks like this issue does affect the user-visible stable behavior of the language: use core::{mem, ptr, sync::atomic::{AtomicU64, Ordering}};
static COUNT: AtomicU64 = AtomicU64::new(0);
struct S;
impl Drop for S {
fn drop(&mut self) {
COUNT.fetch_add(1, Ordering::Relaxed);
}
}
trait Tr {}
impl Tr for S {}
const X: Box<dyn Tr> = unsafe {
mem::transmute::<_, Box<S>>(ptr::dangling_mut::<S>())
};
fn main() {
assert_eq!(0, COUNT.load(Ordering::Relaxed));
let _a: &'static [Box<dyn Tr>; 0] = &[X; 0];
assert_eq!(1, COUNT.load(Ordering::Relaxed));
//~^ Drop was run.
let _b = [X; 0];
assert_eq!(1, COUNT.load(Ordering::Relaxed));
//~^ Drop wasn't run.
} So, yes, it's ours to fix via FCP. Anyone think we need a crater run? For my part, I could go either way on that, and I do want us to fix this, so let's: @rfcbot fcp merge @rustbot labels +I-lang-nominated +needs-fcp cc @rust-lang/lang @rust-lang/lang-advisors |
Team member @traviscross has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns. |
Another fun edge case where this change is visible in user code: This code compiles with this PR, but fails on nightly due to dropping in const use std::rc::Weak;
const X: Weak<i32> = Weak::new();
const Y: [Weak<dyn Send>; 0] = [X; 0]; |
@@ -656,6 +657,24 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { | |||
block.and(rvalue) | |||
} | |||
|
|||
/// Recursively inspect a THIR expression and probe through unsizing | |||
/// operations that can be const-folded today. | |||
fn check_constness(&self, mut ek: &'a ExprKind<'tcx>) -> bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/ek/kind/
I've never seen ek
used for an ExprKind
, kind
is the normal name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Renamed.
The code changes seem fine, speaking as a reviewer who is not at all familiar with the MIR building and relevant lang details. The example from @theemathas might be worth adding to the test case. |
b7b9519
to
c1ab3ed
Compare
@bors try |
This comment has been minimized.
This comment has been minimized.
…<try> Do not materialise X in [X; 0] when X is unsizing a const
Okay, I will request a crater run ahead of the next meeting. |
I will push a commit to include the second test cases. |
Hello team. I would like to request
for a quick triage. 🙇 |
Signed-off-by: Ding Xiang Fei <[email protected]> Co-authored-by: Theemathas Chirananthavat <[email protected]>
Let's do the top 1000, just for more coverage :) @craterbot run mode=build-and-test crates=top-1000 |
👌 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
🚧 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
🎉 Experiment
|
1 regression from |
I.e., all the regressions were spurious. |
Does this relate to #79580 somehow? |
r=me once the lang team has approved. |
@RalfJung Yes. I'm not sure if I'm misunderstanding that issue (which seems to have an outdated description). But this PR is fixing the behavior to be correct, right? |
I'm not sure, I didn't page this back in -- I just remembered seeing something about empty arrays before. |
Fix #143671
It turns out that MIR builder materialise
X
in[X; 0]
into a temporary local whenX
is unsizing aconst
. This led to a confusing call to destructor ofX
when such a destructor is declared. PlaygroundThis patch may miss out other cases that we should avoid materialisation in case of
[X; 0]
. Suggestions to include is most welcome!